<?php
namespace app\home\controller;
use app\home\controller;
use app\home\model\DeviceM;
use myResponse\Res;
use think\Db;
use think\Request;

class Device extends Base{


    //某分类设备 ssfl 设备分类sbfl fl_id
    function categoryDevice(){
        $res =Db::name('sb')->where('ssfl='.input('fl_id'))->select();
        Res::suc('ok',$res);
    }
    function account(){
        $m= new DeviceM;
        $z=$m->accountP();
        Res::suc('ok',$z);
    }
    //巡检设备 调用 PatrolLineDevice
    function searchDeviceByKeyword(){
        $keywords=trim(input('keyword'));
        if(!$keywords)
            Res::errs('请输入设备编号或名称！');
        $map = "`sbdh` LIKE '%{$keywords}%'  or `sbmc` LIKE '%{$keywords}%' ";
        $res =Db::name('sb')->where($map)->select();
        Res::suc('ok',$res);
    }

    //添加
    function addOne(){
        //编码重复
        $map['sbdh'] = input('post.')['sbdh'];
        $res = DeviceM::where($map)->find();
        if($res['sbid'])
            Res::errs('设备编号重复！');

        $m= new DeviceM;
        $this->ret($m->addOne(),0);
    }
    //修改
    function editOne(){
        //编码重复
        $id = input('')['sbid'];
        $map['sbdh'] = input('')['sbdh'];
        $res = DeviceM::where($map)->select();
        if(count($res)>1)
            Res::errs('设备编号重复！');
        if( count($res) ==1 && $res[0]['sbid'] != $id)
            Res::errs('设备编号重复！');
        $m= new DeviceM;
        $this->ret($m->editOne(),1);
    }
    //删除
    function delOne(){
        $m= new DeviceM;
        $r = Db::table('sbbj')->where('sbid = '.input('sbid'))->delete();
        $this->ret($m->delOne(),2);
    }

    function ret($z,$tp){
        $ret=['添加','修改','删除'];
        if($z)
            Res::suc($ret[$tp].'成功！');
        else
            Res::errs($ret[$tp].'失败！');
    }
}